package cn.powerone.scm.service.impl;

import java.util.List;
import cn.powerone.common.utils.DateUtils;
import cn.powerone.scm.domain.ScmCustomerOrders;
import cn.powerone.scm.domain.ScmSupplier;
import cn.powerone.scm.domain.ScmSupplierOrders;
import cn.powerone.scm.service.IScmCustomerOrdersService;
import cn.powerone.scm.service.IScmSupplierOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.powerone.scm.mapper.ScmInvoiceMapper;
import cn.powerone.scm.domain.ScmInvoice;
import cn.powerone.scm.service.IScmInvoiceService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 发票Service业务层处理
 * 
 * @author PowerOne
 * @date 2025-08-22
 */
@Service
public class ScmInvoiceServiceImpl implements IScmInvoiceService 
{
    @Autowired
    private ScmInvoiceMapper scmInvoiceMapper;
    @Autowired
    private IScmCustomerOrdersService scmCustomerOrdersService;
    @Autowired
    private IScmSupplierOrdersService scmSupplierOrdersService;

    /**
     * 查询发票
     * 
     * @param id 发票主键
     * @return 发票
     */
    @Override
    public ScmInvoice selectScmInvoiceById(Long id)
    {
        return scmInvoiceMapper.selectScmInvoiceById(id);
    }

    /**
     * 查询发票列表
     * 
     * @param scmInvoice 发票
     * @return 发票
     */
    @Override
    public List<ScmInvoice> selectScmInvoiceList(ScmInvoice scmInvoice)
    {
        return scmInvoiceMapper.selectScmInvoiceList(scmInvoice);
    }

    /**
     * 新增发票
     * 
     * @param scmInvoice 发票
     * @return 结果
     */
    @Override
    @Transactional
    public int insertScmInvoice(ScmInvoice scmInvoice)
    {
        if (scmInvoice.getOrderType() != 1 && scmInvoice.getOrderType() != 2){
            throw new RuntimeException("订单类型错误");
        }
        if (scmInvoice.getOrderType() == 1){
            ScmCustomerOrders scmCustomerOrders = scmCustomerOrdersService.selectScmCustomerOrdersById(scmInvoice.getOrderId());
            if (scmCustomerOrders == null){
                throw new RuntimeException("订单不存在");
            }
            scmCustomerOrders.setInvoiceAmount(scmCustomerOrders.getInvoiceAmount().add(scmInvoice.getMoneyIncludeTax()));
            scmCustomerOrdersService.updateScmCustomerOrders(scmCustomerOrders);
        }
        if (scmInvoice.getOrderType() == 2){
            ScmSupplierOrders scmSupplierOrders = scmSupplierOrdersService.selectScmSupplierOrdersById(scmInvoice.getOrderId());
            if (scmSupplierOrders == null){
                throw new RuntimeException("订单不存在");
            }
            scmSupplierOrders.setInvoiceAmount(scmSupplierOrders.getInvoiceAmount().add(scmInvoice.getMoneyIncludeTax()));
            scmSupplierOrdersService.updateScmSupplierOrders(scmSupplierOrders);
        }
        scmInvoice.setCreateTime(DateUtils.getNowDate());
        return scmInvoiceMapper.insertScmInvoice(scmInvoice);
    }

    /**
     * 修改发票
     * 
     * @param scmInvoice 发票
     * @return 结果
     */
    @Override
    @Transactional
    public int updateScmInvoice(ScmInvoice scmInvoice)
    {
        if (scmInvoice.getOrderType() != 1 && scmInvoice.getOrderType() != 2){
            throw new RuntimeException("订单类型错误");
        }
        ScmInvoice scmInvoiceOld = scmInvoiceMapper.selectScmInvoiceById(scmInvoice.getId());
        if (scmInvoice.getOrderType() == 1){
            ScmCustomerOrders scmCustomerOrders = scmCustomerOrdersService.selectScmCustomerOrdersById(scmInvoice.getOrderId());
            if (scmCustomerOrders == null){
                throw new RuntimeException("订单不存在");
            }
            scmCustomerOrders.setInvoiceAmount(scmCustomerOrders.getInvoiceAmount().subtract(scmInvoiceOld.getMoneyIncludeTax()).add(scmInvoice.getMoneyIncludeTax()));
            scmCustomerOrdersService.updateScmCustomerOrders(scmCustomerOrders);
        }
        if (scmInvoice.getOrderType() == 2){
            ScmSupplierOrders scmSupplierOrders = scmSupplierOrdersService.selectScmSupplierOrdersById(scmInvoice.getOrderId());
            if (scmSupplierOrders == null){
                throw new RuntimeException("订单不存在");
            }
            scmSupplierOrders.setInvoiceAmount(scmSupplierOrders.getInvoiceAmount().subtract(scmInvoiceOld.getMoneyIncludeTax()).add(scmInvoice.getMoneyIncludeTax()));
            scmSupplierOrdersService.updateScmSupplierOrders(scmSupplierOrders);
        }

        scmInvoice.setUpdateTime(DateUtils.getNowDate());
        return scmInvoiceMapper.updateScmInvoice(scmInvoice);
    }

    /**
     * 批量删除发票
     * 
     * @param ids 需要删除的发票主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteScmInvoiceByIds(Long[] ids)
    {
        ScmInvoice scmInvoice;
        ScmCustomerOrders scmCustomerOrders;
        ScmSupplierOrders scmSupplierOrders;
        for (Long id : ids){
            scmInvoice = scmInvoiceMapper.selectScmInvoiceById(id);
            if (scmInvoice.getOrderType() == 1){
                scmCustomerOrders = scmCustomerOrdersService.selectScmCustomerOrdersById(scmInvoice.getOrderId());
                if (scmCustomerOrders == null){
                    throw new RuntimeException("订单不存在");
                }
                scmCustomerOrders.setInvoiceAmount(scmCustomerOrders.getInvoiceAmount().subtract(scmInvoice.getMoneyIncludeTax()));
                scmCustomerOrdersService.updateScmCustomerOrders(scmCustomerOrders);
            }
            if (scmInvoice.getOrderType() == 2){
                scmSupplierOrders = scmSupplierOrdersService.selectScmSupplierOrdersById(scmInvoice.getOrderId());
                if (scmSupplierOrders == null){
                    throw new RuntimeException("订单不存在");
                }
                scmSupplierOrders.setInvoiceAmount(scmSupplierOrders.getInvoiceAmount().subtract(scmInvoice.getMoneyIncludeTax()));
                scmSupplierOrdersService.updateScmSupplierOrders(scmSupplierOrders);
            }
        }
        return scmInvoiceMapper.deleteScmInvoiceByIds(ids);
    }

    /**
     * 删除发票信息
     * 
     * @param id 发票主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteScmInvoiceById(Long id)
    {
        return scmInvoiceMapper.deleteScmInvoiceById(id);
    }
}
